<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.8.7/babel.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
    <script crossorigin="anonymous" src="/__dist__/botframework-webchat-fluent-theme.production.min.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="text/babel">
      run(async function () {
        const { WebChat } = window; // Imports in UMD fashion.

        const { directLine, store } = testHelpers.createDirectLineEmulator();

        const render = attachmentMiddleware =>
          WebChat.renderWebChat({ attachmentMiddleware, directLine, store }, document.getElementById('webchat'));

        render(() => next => (...args) => {
          const result = next(...args);

          return (
            <div>
              {result}
              <div>
                <button type="button">Hello, World!</button>
                <button type="button">Aloha!</button>
              </div>
            </div>
          );
        });

        await pageConditions.uiConnected();

        await directLine.emulateIncomingActivity({
          id: 'a-00001',
          text: 'Aloha!',
          type: 'message'
        });

        // WHEN: The message activity arrives.
        await pageConditions.numActivitiesShown(1);

        // THEN: Should render the message with 2 buttons.
        await host.snapshot();

        // WHEN: Focusing on the "Hello, World!" button.
        pageElements.sendBoxTextBox().focus();

        await host.sendShiftTab(3);
        await host.sendKeys('ENTER');

        // THEN: Should focus on the "Hello, World!" button.
        await host.snapshot();

        // WHEN: The "Hello, World!" button is disabled via tabIndex.
        render(() => next => (...args) => {
          const result = next(...args);

          return (
            <div>
              {result}
              <div>
                <button aria-disabled="true" tabIndex={-1} type="button">
                  Hello, World!
                </button>
                <button type="button">Aloha!</button>
              </div>
            </div>
          );
        });

        // THEN: Should continue focus on the "Hello, World!" button.
        expect(document.activeElement.textContent).toBe('Hello, World!');

        // WHEN: TAB key is pressed.
        await host.sendTab();

        // THEN: Should focus on the "Aloha!" button.
        expect(document.activeElement.textContent).toBe('Aloha!');
        await host.snapshot();

        // WHEN: TAB key is pressed again.
        await host.sendTab();

        // THEN: Should continue to focus on the "Aloha!" button.
        expect(document.activeElement.textContent).toBe('Aloha!');
        await host.snapshot();
      });
    </script>
  </body>
</html>
